package com.gamebase.net.rpc
{
	import flash.utils.ByteArray;
	
	public class PacketHeader
	{
		public static const headerLength:uint = 15;
		
		
		
		
		public var length:uint;
		public var iid:uint;//服务器ID
		public var sid:uint;//服务器上的不同 类对象ID
		public var mid:uint;//方法ID
		public var tsn:uint;//某次请求ID
		public var st:uint;//编码类型，用于表示不同希望平台
		public var flag:uint;//返回操作方式
		public var roleID:uint;//
		
		public var serializer:Serializer; //包
		public var buffer:ByteArray;
		
		public function PacketHeader()
		{
			length = 0;
			iid = 0;
			sid = 1;
			mid = 0;
			tsn = 0;
			st = 2;
			flag = 1;
			roleID = 0;
			
			buffer = new ByteArray();
			serializer = new Serializer(buffer);
			
		}
		
		public function reset():void
		{
			buffer.position = 0;
			buffer.length = 0;
			length = 0;
			serializer.hasError = false;
		}
		
		public function decodeLength():void
		{
			length = buffer.readUnsignedShort();
		}
		
		public function encodeLength():void
		{
			length = buffer.length;
			serializer.writeUnsignedShort(length);
		}
		
		public function encode():void
		{
			length = buffer.length;
			serializer.writeUnsignedShort(length);
			
			serializer.writeUnsignedByte(flag);
			serializer.writeUnsignedByte(iid);
			serializer.writeUnsignedByte(sid);			
			serializer.writeUnsignedShort(mid);
			serializer.writeUnsignedInt(tsn);
			serializer.writeUnsignedInt(roleID);
		}
		
		public function decode():void
		{
			length = serializer.readUnsignedShort();
			flag = serializer.readUnsignedByte();
			iid = serializer.readUnsignedByte();
			sid = serializer.readUnsignedByte();			
			mid = serializer.readUnsignedShort();
			tsn = serializer.readUnsignedInt();
			roleID = serializer.readUnsignedInt();
		}
		
	}
}